﻿#include "FITKRadiossAdaptorSurfacePart.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossCase.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossMeshModel.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossSurfaceSeg.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossNodesParts.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractElement.h"

#include "FITKRadiossRadReader.h"
#include "RadKeyLineSpliter.h"
#include "RadReadInformationMapper.h"

namespace Radioss
{
    QString FITKRadiossAdaptorSurfacePart::getAdaptorClass()
    {
        return "Radioss::FITKRadiossSurfacePart";
    }
    bool FITKRadiossAdaptorSurfacePart::adaptR()
    {
        //错误判断，获取必要的工具类
        if (_caseData == nullptr || _reader == nullptr) return false;
        RadKeyLineSpliter* spliter = _reader->getKeyLineSpliter();
        if (!spliter)return false;
        RadReadInformationMapper* mapper = _reader->getReadRadInformationMapper();
        if (!mapper)return false;
        Radioss::FITKRadiossMeshModel* radiossMeshModel = _caseData->getMeshModel();
        if (!radiossMeshModel)return false;
        Interface::FITKComponentManager* componentManager = radiossMeshModel->getComponentManager();
        if (!componentManager)return false;

        //读取surface ID
        const int surfaceID = spliter->getId(0);
        if (surfaceID <= 0) return false;

        const QString setName = _reader->readLine();
        if (setName.isEmpty()) return false;

        //判断面组类型
        int type = -1;
        if (spliter->getKeys() == "/SURF/PART")
            type = 1;
        else if (spliter->getKeys() == "/SURF/PART/ALL")
            type = 2;
        else if (spliter->getKeys() == "/SURF/PART/EXT")
            type = 3;
        if (type < 0)return false;

        Radioss::FITKRadiossSurfaceSeg * surfaceSeg = new Radioss::FITKRadiossSurfaceSeg();
        surfaceSeg->setModel(radiossMeshModel);

        //读取Part列表
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine();
            //读取结束标记
            if (line.startsWith("/"))
            {
                _reader->backLine();
                break;
            }
            switch (type)
            {
            case 1: {
                QStringList partStrList = line.split(" ", Qt::SkipEmptyParts);
                do {
                    for (int i = 0; i < partStrList.size(); ++i) {
                        //PartId
                        int partRadID = partStrList[i].toInt();
                        this -> setShellPartToSurface(partRadID, surfaceSeg);
                    }
                    line = _reader->readLine();
                    partStrList = line.split(" ", Qt::SkipEmptyParts);
                    if (partStrList.isEmpty())
                        break;
                } while (partStrList[0].toInt() > 0);
                _reader->backLine();
                break;
            }
            case 2: {
                break;
            }
            case 3: {
                break;

            }
            default:
                break;
            }
        }
        if (surfaceSeg->getAllElement().isEmpty()) {
            delete surfaceSeg;
            surfaceSeg = nullptr;
            return false;
        }

        surfaceSeg->setDataObjectName(setName);

        //设置单元面集到RadiossMeshModel
        componentManager->appendDataObj(surfaceSeg);
        mapper->insertSurfaceSetIDMap(surfaceID, surfaceSeg->getDataObjectID());

        return true;
    }

    void FITKRadiossAdaptorSurfacePart::setShellPartToSurface(int radID, Radioss::FITKRadiossSurfaceSeg * surfaceSeg)
    {
        if (!surfaceSeg)return;
        RadReadInformationMapper* mapper = _reader->getReadRadInformationMapper();
        if (!mapper)return ;
        FITKRadiossPart* part = mapper->getRadiossPartByFilePartID(radID,false);
        if(!part)return ;
        int count = part->getElementCount();
        for (int i =0;i < count; ++i)
        {
            Interface::FITKAbstractElement* ele = part->getElementAt(i);
            if (!ele || ele->getNodeCount() < 3)continue;
            if(ele->getElementDim() != 2) continue;
            surfaceSeg->addElementSurface(ele->getEleID(), 0);
        }
    }

}